package com.paireach.szwolaiyun.sz_common_widget.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.widget.Toast;

import com.paireach.szwolaiyun.sz_common_widget.activity.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.BaseEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpErrorEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpSuccessEvent;

/**
 * use eventbus to initial basefragment
 */
public class BaseFragment extends Fragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public final void onEventCallBack(BaseEvent event) {
        if (event instanceof HttpErrorEvent) {
            //mark error
            httpErrorEvent((HttpErrorEvent) event);
        } else if (event instanceof HttpSuccessEvent) {
            httpSuccessEvent((HttpSuccessEvent) event);
        } else {
            applicationEvent((AppEvent) event);
        }
    }

    /**
     * 处理网络失败/错误请求
     * <p>直接判断HttpEvent的RequestTag即可
     *
     * @param event 错误事件
     */
    public void httpErrorEvent(HttpErrorEvent event) {

    }

    /**
     * 处理网络成功请求
     * <p>直接判断HttpEvent的RequestTag即可
     *
     * @param event 成功事件
     */
    public void httpSuccessEvent(HttpSuccessEvent event) {

    }

    /**
     * 处理app内部事件
     *
     * @param event app内部事件
     */
    public void applicationEvent(AppEvent event) {

    }

    public void showToast(String message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    public void showProgressDialog() {
        try {
            ((BaseActivity) getActivity()).showProgressDialog();
        } catch (ClassCastException e) {
            Log.w("BaseFragment","trying to show progress dialog failed.Parent activity:"+getActivity().getClass().getName()+" never extends BaseActivity!");
        } catch (NullPointerException e) {
            Log.w("BaseFragment",getClass().getName()+"does not attach to activity");
        }
    }

    public void dismissProgressDialog() {
        try {
            ((BaseActivity) getActivity()).dismissProgressDialog();
        } catch (ClassCastException e) {
            Log.w("BaseFragment","trying to dismiss progress dialog failed.Parent activity:"+getActivity().getClass().getName()+" never extends BaseActivity!");
        } catch (NullPointerException e) {
            Log.w("BaseFragment",getClass().getName()+"does not attach to activity");
        }
    }

}
